package cn.tedu.qxhouserent.house.controller;

import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.house.pojo.entity.House;
import cn.tedu.qxhouserent.house.pojo.param.*;
import cn.tedu.qxhouserent.house.pojo.vo.*;
import cn.tedu.qxhouserent.house.service.HouseService;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/house")
@Api(tags = "房源管理模块")
@Slf4j
@Validated
public class HouseController {
    @Autowired
    private HouseService houseService;

    @PostMapping("/add-new/lease-house")
    @ApiOperation("添加出租房源")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/pms/house/add-new')")
    public JsonResult addNew(@Valid LeaseHouseAddNewParam leaseHouseAddNewParam) {
        log.debug("开始处理【添加出租房源】的请求，参数：{}", leaseHouseAddNewParam);
        houseService.addLeaseHouse(leaseHouseAddNewParam);
        log.debug("处理【添加出租房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/add-new/new-house")
    @ApiOperation("添加新房源")
    @ApiOperationSupport(order = 110)
    @PreAuthorize("hasAuthority('/pms/house/add-new')")
    public JsonResult addNew(@Valid NewHouseAddNewParam newHouseAddNewParam) {
        log.debug("开始处理【添加新房源】的请求，参数：{}", newHouseAddNewParam);
        houseService.addNewHouse(newHouseAddNewParam);
        log.debug("处理【添加新房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/add-new/second-hand-house")
    @ApiOperation("添加二手房源")
    @ApiOperationSupport(order = 120)
    @PreAuthorize("hasAuthority('/pms/house/add-new')")
    public JsonResult addNew(@Valid SecondHandHouseAddNewParam secondHandHouseAddNewParam) {
        log.debug("开始处理【添加二手房源】的请求，参数：{}", secondHandHouseAddNewParam);
        houseService.addSecondHandHouse(secondHandHouseAddNewParam);
        log.debug("处理【添加二手房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/delete/lease-house")
    @ApiOperation("根据id删除出租房源")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "房源ID",required = true,dataType = "long"),

    })
    @PreAuthorize("hasAuthority('/pms/house/delete')")
    public JsonResult deleteLeaseHouse(@Range(min = 1,message = "根据ID删除房源失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除出租房源】的请求，参数：{}", id);
        houseService.deleteLeaseHouseById(id);
        log.debug("处理【根据id删除出租房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/delete/new-house")
    @ApiOperation("根据id删除新房源")
    @ApiOperationSupport(order = 210)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "房源ID",required = true,dataType = "long"),
    })
    @PreAuthorize("hasAuthority('/pms/house/delete')")
    public JsonResult deleteNewHouse(@Range(min = 1,message = "根据ID删除房源失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除新房源】的请求，参数：{}", id);
        houseService.deleteNewHouseById(id);
        log.debug("处理【根据id删除新房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/delete/second-hand-house")
    @ApiOperation("根据id删除二手房源")
    @ApiOperationSupport(order = 220)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "房源ID",required = true,dataType = "long"),
    })
    @PreAuthorize("hasAuthority('/pms/house/delete')")
    public JsonResult deleteSecondHandHouse(@Range(min = 1,message = "根据ID删除房源失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除二手房源】的请求，参数：{}", id);
        houseService.deleteSecondHandHouseById(id);
        log.debug("处理【根据id删除二手房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update/lease-house")
    @ApiOperation("修改出租房源")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/pms/house/update')")
    public JsonResult updateLeaseHouseById(@Range(min = 1,message = "根据ID更新房源失败，请提交合法的ID值！")
                                     @RequestParam Long id,
                                           @Valid LeaseHouseUpdateInfoParam leaseHouseUpdateInfoParam) {
        log.debug("开始处理【修改房源】的请求，参数：{}", id, leaseHouseUpdateInfoParam);
        houseService.updateLeaseHouseById(id, leaseHouseUpdateInfoParam);
        log.debug("处理【修改房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update/new-house")
    @ApiOperation("修改新房源")
    @ApiOperationSupport(order = 310)
    @PreAuthorize("hasAuthority('/pms/house/update')")
    public JsonResult updateNewHouseById(@Range(min = 1,message = "根据ID更新房源失败，请提交合法的ID值！")
                                     @RequestParam Long id,
                                     @Valid NewHouseUpdateInfoParam newHouseUpdateInfoParam) {
        log.debug("开始处理【修改房源】的请求，参数：{}", id, newHouseUpdateInfoParam);
        houseService.updateNewHouseById(id, newHouseUpdateInfoParam);
        log.debug("处理【修改房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update/second-hand-house")
    @ApiOperation("修改二手房源")
    @ApiOperationSupport(order = 320)
    @PreAuthorize("hasAuthority('/pms/house/update')")
    public JsonResult updateSecondHandHouseById(@Range(min = 1,message = "根据ID更新房源失败，请提交合法的ID值！")
                                     @RequestParam Long id,
                                     @Valid SecondHandHouseUpdateInfoParam secondHandHouseUpdateInfoParam) {
        log.debug("开始处理【修改房源】的请求，参数：{}", id, secondHandHouseUpdateInfoParam);
        houseService.updateSecondHandHouseById(id, secondHandHouseUpdateInfoParam);
        log.debug("处理【修改房源】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("推荐房源")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/house/update')")
    public JsonResult setEnable(@PathVariable Long id) {
        log.debug("开始处理【推荐房源】的请求，参数：{}", id);
        houseService.setRecom(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("不推荐房源")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/house/update')")
    public JsonResult setDisable(@PathVariable Long id) {
        log.debug("开始处理【不推荐房源】的请求，参数：{}", id);
        houseService.setNotRecom(id);
        return JsonResult.ok();
    }

    @GetMapping("/list/lease-house")
    @ApiOperation("查询出租房源列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listLeaseHouse(@Range(min = 1, message = "查询房源列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询房源列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<LeaseHouseListItemVO> pageData = houseService.listLeaseHouse(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard/lease-house")
    @ApiOperation("根据ID查询出租房源详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源ID", required = true, dataType = "long")
    })
    public JsonResult getStandardLeaseHouse(@Range(min = 1, message = "根据ID查询房源详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询房源详情】的请求，参数：{}", id);
        return JsonResult.ok(houseService.getStandardLeaseHouse(id));
    }

    @GetMapping("/list/new-house")
    @ApiOperation("查询新房源列表")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listNewHouse(@Range(min = 1, message = "查询房源列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询房源列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<NewHouseListItemVO> pageData = houseService.listNewHouse(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard/new-house")
    @ApiOperation("根据ID查询新房源详情")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源ID", required = true, dataType = "long")
    })
    public JsonResult getStandardNewHouse(@Range(min = 1, message = "根据ID查询房源详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询房源详情】的请求，参数：{}", id);
        NewHouseStandardVO result = houseService.getStandardNewHouse(id);
        return JsonResult.ok(result);
    }

    @GetMapping("/list/second-hand-house")
    @ApiOperation("查询二手房源列表")
    @ApiOperationSupport(order = 460)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listSecondHandHouse(@Range(min = 1, message = "查询房源列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询房源列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<SecondHandHouseListItemVO> pageData = houseService.listSecondHandHouse(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/standard/second-hand-house")
    @ApiOperation("根据ID查询二手房源详情")
    @ApiOperationSupport(order = 450)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房源ID", required = true, dataType = "long")
    })
    public JsonResult getStandardSecondHandHouse(@Range(min = 1, message = "根据ID查询房源详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询房源详情】的请求，参数：{}", id);
        SecondHandHouseStandardVO result = houseService.getStandardSecondHandHouse(id);
        return JsonResult.ok(result);
    }

    @GetMapping("/list/recom")
    @ApiOperation("查询推荐出租房源列表")
    @ApiOperationSupport(order = 460)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listRecom() {
        log.debug("开始处理【查询推荐出租房源列表】的请求.");
        List<RecomHouseListItemVO> listRecom = houseService.listRecom();
        List<RecomHouseListItemVO> recomHouseListItemVOS = listRecom.subList(0, 9);
        return JsonResult.ok(recomHouseListItemVOS);
    }

    @GetMapping("/list/recom/less")
    @ApiOperation("查询房源列表")
    @ApiOperationSupport(order = 460)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listRecomLess() {
        log.debug("开始处理【查询推荐出租房源列表】的请求.");
        List<RecomHouseListItemVO> listRecom = houseService.listRecom();
        List<RecomHouseListItemVO> recomHouseListItemVOS = listRecom.subList(0, 3);
        return JsonResult.ok(recomHouseListItemVOS);
    }



    @GetMapping("/list/ershourecom")
    @ApiOperation("查询推荐二手房源列表")
    @ApiOperationSupport(order = 461)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult ershoulistRecom() {
        log.debug("开始处理【查询二手推荐房源列表】的请求.");
        List<RecomHouseListItemVO> ershoulistRecom = houseService.ershoulistRecom();
        List<RecomHouseListItemVO> recomHouseListItemVOS = ershoulistRecom.subList(0, 9);
        return JsonResult.ok(recomHouseListItemVOS);
    }

    @GetMapping("/list/ershourecom/less")
    @ApiOperation("查询二手房源列表")
    @ApiOperationSupport(order = 461)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult ershoulistRecomLess() {
        log.debug("开始处理【查询二手推荐房源列表】的请求.");
        List<RecomHouseListItemVO> ershoulistRecom = houseService.ershoulistRecom();
        List<RecomHouseListItemVO> recomHouseListItemVOS = ershoulistRecom.subList(0, 3);
        return JsonResult.ok(recomHouseListItemVOS);
    }

    @GetMapping("/list/newrecom")
    @ApiOperation("查询推荐新房房源列表")
    @ApiOperationSupport(order = 462)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult mailistRecom() {
        log.debug("开始处理【查询买推荐房源列表】的请求.");
        List<RecomHouseListItemVO> mailistRecom = houseService.mailistRecom();
        List<RecomHouseListItemVO> recomHouseListItemVOS = mailistRecom.subList(0, 9);
        return JsonResult.ok(recomHouseListItemVOS);
    }

    @GetMapping("/list/newrecom/less")
    @ApiOperation("查询新房房源列表")
    @ApiOperationSupport(order = 462)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult mailistRecomLess() {
        log.debug("开始处理【查询买推荐房源列表】的请求.");
        List<RecomHouseListItemVO> mailistRecom = houseService.mailistRecom();
        List<RecomHouseListItemVO> recomHouseListItemVOS = mailistRecom.subList(0, 3);
        return JsonResult.ok(recomHouseListItemVOS);
    }

    @GetMapping("/list/cha")
    @ApiOperation("搜索框")
    @ApiOperationSupport(order = 470)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" ,value = "要查询的详细信息", paramType = "query")
    })
    public JsonResult cha(String name, Long hcid){

        List<HouseListItemVO> l = houseService.cha(name,hcid);
        return JsonResult.ok(l);
    }

    @GetMapping("/list/chaxiang")
    @ApiOperation("房源的多条件查询")
    @ApiOperationSupport(order = 480)
    public JsonResult chaxiang(HouseSelectParam houseSelectParam,Integer page){
        log.debug("开始处理【多条件查询房源】的请求，参数houseSelectParam: {},page: {}", houseSelectParam,page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<HouseListItemVO> chaxiang = houseService.chaxiang(houseSelectParam, page);
        return JsonResult.ok(chaxiang);
    }
}
